package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.dao.jdbc.ExaminationRoomJdbc;
import com.wuseguang.springcloud.dao.repository.ExaminationRoomRepository;
import com.wuseguang.springcloud.dao.repository.ExamsRepository;
import com.wuseguang.springcloud.entity.ExaminationRoom;
import com.wuseguang.springcloud.entity.Exams;
import com.wuseguang.springcloud.service.ExaminationRoomService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.JsonUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考场表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-28
 */
@Service
public class ExaminationRoomServiceImpl implements ExaminationRoomService {

    private Logger logger = LoggerFactory.getLogger(this.toString());
    @Autowired
    private ExaminationRoomRepository examinationRoomRepository;
    @Autowired
    private ExamsRepository examsRepository;
    @Autowired
    private ExaminationRoomJdbc examinationRoomJdbc;
    @Autowired
    private ClassCardFieldService service;
    //推送接口
    @Autowired
    private ClassCardService classCardService;

    @Override
    public Result addExaminationRoom(ExaminationRoom examinationRoom, String teacherId) {

        logger.info("添加考场");
        //查询考场是否存在
        String sql ="select count(1) `count` from examination_room  where school_id = ? and `name`=?";
        Integer count = examinationRoomJdbc.queryCount(sql,examinationRoom.getSchoolId(),examinationRoom.getName());
        if(count>0){
           return new Result(CodeUtil.THE_EXAMINATION_ROOM_ALREADY_EXISTS);
        }


        examinationRoom.setPattern(0);
        examinationRoom.setTeacherId(Integer.valueOf(teacherId));
        examinationRoom.setAddTime(new Date());
        Integer id = examinationRoomRepository.save(examinationRoom).getId();
        logger.info("考场添加成功,接下来添加考试,考场id = {}", id);


        logger.info("添加试");
        //获取考试信息
        List<Exams> examsList = JsonUtil.jsonToList(examinationRoom.getExamsList(), Exams.class);
        for (Exams exams : examsList) {
            exams.setExaminationRoomId(id);
            exams.setSchoolId(examinationRoom.getSchoolId());
            exams.setAddTime(new Date());
            examsRepository.save(exams);
        }
        logger.info(examinationRoom.getName() + "的所有考试添加成功");


        return new Result();
    }

    @Override
    public Result findExaminationRoom(String schoolId, String teacherId, String name) {

        logger.info("查询考场");
        //查询考场sql
        String sql = "select er.id, " +
                " er.name, " +
                " er.pattern, " +
                " er.teacher_id, " +
                " DATE_FORMAT(e.startTime, '%Y-%m-%d %T') startTime, " +
                " DATE_FORMAT(e.endTime, '%Y-%m-%d %T') endTime, " +
                " t.`name` teacherName, " +
                " t.head_portrait " +
                "  from examination_room er " +
                "  left join( select min(start_time) startTime, max(end_time) endTime, examination_room_id, id " +
                "  from exams " +
                " where school_id= ? " +
                " GROUP BY examination_room_id) e on(e.examination_room_id= er.id) " +
                "  LEFT join teacher t on(t.id= er.teacher_id) " +
                " where er.school_id= ? " +
                "   and er.name like '%"+name+"%'";
        List<Map<String, Object>> list = examinationRoomJdbc.queryListMap(sql, schoolId, schoolId);

        return new Result(list);
    }

    @Override
    public Result delteExaminationRoom(Integer id) {
        logger.info("删除考场");
        try {
            examinationRoomRepository.deleteById(id);
            examsRepository.deleteByExaminationRoomId(id);
        } catch (Exception e) {

        }
        return new Result();
    }

    @Override
    public List<Integer> openTheExaminationRoom(Integer id, Integer pattern, String schoolId) {
        logger.info("开启考场模式");

        //如果是开启考场就把其他的考场关掉
        if (1 == pattern) {
            String sql = "UPDATE `examination_room` SET `pattern` = 0  WHERE school_id=?";
            examinationRoomJdbc.updateByParameter(sql, schoolId);

        }
        //开始考场模式
        String sql = "UPDATE `examination_room` SET `pattern` = ?  WHERE id=?";
        examinationRoomJdbc.updateByParameter(sql, pattern, id);

        //查询这个考场的所有教室
        sql = "select classroom_id from exams where examination_room_id = ? GROUP BY classroom_id  ";
        List<Integer> list = examinationRoomJdbc.queryListInteger(sql, new Object[]{id});

        //修改心跳字段 圖片
        service.classRoomAndCards(list, "examflag", pattern.toString());
        classCardService.classRoomAndCards(list);
        return list;
    }
}
